<?php
/*
	会员管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
	array('op'=>'list','title'=>'业务员管理'),
	//array('op'=>'edit','title'=>'添加/编辑业务员'),
);
set_html('tabs',$tabs);
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['mid']){
		$params['dian_mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}else{
		$params['dian_mid >']=0;
	}
	$psize=20;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'createtime desc',
	);
	$list=$this->get_list('member',$parameter,$total);
	if($list){
		$action=$this->setting['become_reg']?'apply':'index';
		foreach($list as $k=>&$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			if(!$item['member']['id']){
				pdo_delete('jy_area_fee_member',array('mid'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']));	
				unset($list[$k]);
			}
			$item['dian_member']=cfc('core')->member_show($item['dian_mid']);
			$item['level_id']=pdo_getcolumn('jy_area_fee_member',array('mid'=>$item['dian_mid'],'uniacid'=>$_SESSION['uniacid']),'level_id');
			$item['agent']=cfc('core')->member_show($item['agent_id'],'',array('nickname'=>'总店'));
			$item['from_os']=pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['mid']),'from_os');
			$item['from_os']=$item['from_os']?get_ostitle()[$item['from_os']]:'微信';
			$item['url']=custom_url('jy_area_fee/'.$action.'//sale_id='.$item['mid'].'&fromuser='.$item['mid'],true);
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','key'=>'mid','tip'=>'<red>[温馨提示]：关于匿名用户的解释：正常认证服务号，没有关闭获取粉丝详细的情况下，是不会存在匿名用户的；可能小程序出现匿名用户的情况居多：原因是：小程序官方不允许首次访问弹窗获取粉丝详细。但是为了上下级的准确性，在访问小程序首页的时候就产生了下级关系！这时候就会出现匿名用户的情况。但是只要这个匿名用户访问了购物车，商品，会员中心等任何页面。就会弹窗获取粉丝信息。这时候匿名用户就会变成带昵称头像的会员了！</red>','key'=>'mid','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'mid','title'=>'ID','type'=>'string'),
			array('name'=>'member','title'=>'会员信息','type'=>'member'),
			array('name'=>'dian_member','title'=>'所属经销商','type'=>'member'),
			array('name'=>'level_id','title'=>'经销商等级','type'=>'select','options'=>'levels'),
			array('name'=>'from_os','title'=>'来源','type'=>'string'),
			array('name'=>'url','title'=>'推广码','type'=>'ewm'),
			array('name'=>'url','title'=>'推广链接','type'=>'copy'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'href','op'=>'member','title'=>'客户明细'),
			array('type'=>'href','op'=>'order','title'=>'成单明细'),
			array('type'=>'href','op'=>'change','title'=>'修改经销商'),
		)),
		'search'=>array(
			array('type'=>'choose','width'=>15,'action'=>'core/member.get_member/','name'=>'mid','title'=>'选择会员'),
		),
	);
	$return['htmls']=$htmls;
	$return['agent_status']=$this->agent_status;
	$return['levels']=$this->getall('level',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['levels']['0']=array('id'=>'0','title'=>'默认等级');
	$return['types']=array(
		'0'=>'普通会员',
		'1'=>'经销商',
	);
	exi($return);
}
if($_RQ['op']=='edit'){
	$mid=intval($_RQ['mid']);
	if($mid){
		$item=$this->get('member',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array();
	}
    $return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'业务员添加',
		'edit'=>array(
			array('type'=>'choose','name'=>'dian_mid','title'=>'所属经销商','action'=>'jy_area_fee/get_agent/'),
			array('type'=>'choose','name'=>'mid','title'=>'设置业务员','action'=>'jy_area_fee/get_member/'),
		),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'dian_mid'=>intval($postdata['dian_mid']),
		'is_agent'=>1,
		'agenttime'=>TIMESTAMP,
	);
	$mid=intval($postdata['mid']);
	$item=$this->get('member',array('mid'=>$mid),array('mid','agent_id'));
	if($item['mid']){
		$this->update('member',$data,array('mid'=>$mid));
		exi('设置成功！','','list');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['mid']=$mid;
		$this->insert('member',$data);
		exi('设置成功！','error');
	}
}
if($_RQ['op']=='change'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$mid=intval($post['mid']);
		$dian_mid=intval($post['dian_mid']);
		if($dian_mid==$mid){
			exi('不能选择自己！','error');
		}
		$this->update('member',array('dian_mid'=>$dian_mid),array('mid'=>$mid));
		exi('修改推荐人成功！','','list');
	}
	$item=$this->get('member',array('mid'=>intval($_RQ['mid'])),array('mid','agent_id'));
	if(!$item){
		exi('指定会员不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'修改经销商',
		'edit'=>array(
			array('type'=>'choose','name'=>'dian_mid','title'=>'设置经销商','action'=>'jy_area_fee/get_agent/','help'=>'填写0表示设置为总店'),
		),
	);
	exi($return);
}
if($_RQ['op']=='member'){	
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'agent_id'=>intval($_RQ['mid']));
	$return['params']['mid']=$_RQ['mid'];
	//是否经销商
	if(isset($_RQ['is_agent'])){
		$params['is_agent']=intval($_RQ['is_agent']);
		$return['params']['is_agent']=$_RQ['is_agent'];
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
		$return['params']['createtime']=$time_range;
	}
	$psize=20;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'createtime desc',
	);
	$list=$this->get_list('member',$parameter,$total);
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			//$item['agent']=cfc('core')->member_show($item['agent_id'],'',array('nickname'=>'总店'));
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export2',
		'list'=>array('data'=>'list','key'=>'mid','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'member','title'=>'客户','type'=>'member'),
			array('name'=>'level_id','title'=>'客户等级','type'=>'select','options'=>'levels'),
			array('name'=>'agenttime','title'=>'成为经销商时间','type'=>'time'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
			array('name'=>'stop','title'=>'状态','type'=>'radio','options'=>'agent_status'),
		)),
		'search'=>array(
			array('type'=>'choose','width'=>15,'action'=>'core/member.get_member/','name'=>'mid','title'=>'选择上级'),
			array('name'=>'createtime','width'=>15,'title'=>'加入时间','type'=>'time_range'),
		),
	);
	$return['htmls']=$htmls;
	$return['agent_status']=$this->agent_status;
	$return['levels']=$this->getall('level',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['levels']['0']=array('id'=>'0','title'=>'默认等级');
	exi($return);
}
if($_RQ['op']=='order'){	
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$sale_id=intval($_RQ['mid'])?:$_RQ['sale_id'];
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($sale_id){
		$params['sale_id']=$sale_id;
		$return['params']['sale_id']=$sale_id;
	}
	if($_RQ['ordersn']){
		$params['ordersn']=$_RQ['ordersn'];
		$return['params']['ordersn']=$_RQ['ordersn'];
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
		$return['params']['createtime']=$time_range;
	}
	$psize=20;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'createtime desc',
	);
	$list=$this->get_list('order',$parameter,$total);
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			//$item['agent']=cfc('core')->member_show($item['agent_id'],'',array('nickname'=>'总店'));
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export2',
		'list'=>array('data'=>'list','key'=>'mid','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'member','title'=>'会员信息','type'=>'member'),
			array('name'=>'ordersn','title'=>'订单号','type'=>'string'),
			array('name'=>'order_price','title'=>'订单金额','type'=>'string'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
		)),
		'search'=>array(
			array('type'=>'string','width'=>15,'name'=>'ordersn','title'=>'订单号'),
			array('name'=>'createtime','width'=>15,'title'=>'创建时间','type'=>'time_range'),
		),
	);
	$return['htmls']=$htmls;
	$return['agent_status']=$this->agent_status;
	$return['levels']=$this->getall('level',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['levels']['0']=array('id'=>'0','title'=>'默认等级');
	exi($return);
}
?>